﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EzDesk.Utility.TimerService
{
    public class EverydayTimerTask : TimerTaskBase
    {
        protected EverydayTimerTask(string taskName, TimeSpan dayExecuteTime)
            : base(taskName)
        {
            this.DayExecuteTime = dayExecuteTime;
            this.ExecuteTime = getNextExecuteTime();
        }

        protected DateTime NextDay;

        public TimeSpan DayExecuteTime
        {
            get;
            protected set;
        }

        public sealed override DateTime ExecuteTime
        {
            get;
            protected set;
        }

        /// <summary>
        /// 重载获得下一次执行时间的方法，得到第二天的这个时间
        /// </summary>
        /// <returns></returns>
        protected override sealed DateTime getNextExecuteTime()
        {
            var now = DateTime.Now;
            var executeTimeToday = DateTime.Now.Date.Add(DayExecuteTime);
            DateTime nextDay = now.Date;
            // 如果今日已经过了执行时间，则下一天
            if (DateTime.Now > executeTimeToday)
            {
                nextDay = nextDay.AddDays(1);
            }
            NextDay = nextDay;

            return nextDay.Date.Add(DayExecuteTime);
        }
    }
}
